#!/usr/X11R6/bin/python3.1
import cgi
import cgitb
import os
from pygallib import usersession
from pygallib import layoutlib
from pygallib import albumlib
from pygallib import userlib
from pygallib import picturelib

# Enable debug print
cgitb.enable()

# Printing Headers
logged_in = usersession.logged_in()

headers = {}
headers["Content-type"] = "text/html"
layoutlib.print_http_headers(headers)
layoutlib.print_page_open("PyGallery - Home")
print("<body>")
menu = []
menu.append(("Home", "gallery.cgi"))
if logged_in:
  menu.append(("Logout", "logout.cgi"))
  menu.append(("Account Settings", "account_settings.cgi"))
else:
  menu.append(("Login", "login.cgi"))
layoutlib.print_page_menu(menu)

# Get the fields from url
fields = cgi.FieldStorage()

if logged_in:
  user = usersession.get_session_user()
  if "album_name" in fields:
    album_name = fields["album_name"].value
    if len(album_name)==0:
      print("<h4>Error: Album had no name</h4></br>")
      print("""Create new album:</br>""")
      print("""
      </br>Album name:
      <form action="create_album.cgi" method="post" enctype="multipart/form-data">
      <input type="text" maxlength=512 name="album_name" size="30"/><br />
      </br>Visibility:</br>
      <input type="radio" name="is_public" value="1" CHECKED>public</input><br/>
      <input type="radio" name="is_public" value="0">private</input><br/>
      <input type="submit" value="Submit" />
      </form>
      """ )
    else:
      is_public = int(fields["is_public"].value)
      # Add that album to albums table
      new_album_id = albumlib.add_album(user["user_id"], album_name, is_public)
      print('<p>Album <a href="album.cgi?id=%d">%s</a> created, click ' % (new_album_id, album_name) +
        '<a href="gallery.cgi">here</a> for the gallery.')
  else:
    print("""Create new album:</br>""")
    print("""
    </br>Album name:
    <form action="create_album.cgi" method="post" enctype="multipart/form-data">
    <input type="text" maxlength=512 name="album_name" size="30"/><br />
    </br>Visibility:</br>
    <input type="radio" name="is_public" value="1" CHECKED>public</input><br/>
    <input type="radio" name="is_public" value="0">private</input><br/>
    <input type="submit" value="Submit" />
    </form>
    """ )
layoutlib.print_page_close()
